/*
 * seqio.c
 *
 *  Created on: Oct 30, 2013
 *      Author: jiang
 */

#include <zlib.h>
#include <stdio.h>
#include "seqio.h"
#include "kseq.h"
#include "bntseq.h"
#include "utils.h"
#include "global.h"
#include "lookup.h"




void free_read_seq(int n_seqs, read_t *seqs)
{
	int i;
	for (i = 0; i != n_seqs; ++i) {
		read_t *p = seqs + i;
		free(p->name);
		free(p->seq);  free(p->qual);
		free(p->cigar);
	}
	free(seqs);
}


seqio_t *seq_open(const char *fn)
{
	gzFile fp;
	seqio_t *bs;
	bs = (seqio_t*)calloc(1, sizeof(seqio_t));
	fp = xzopen(fn, "r");
	bs->ks = kseq_init(fp);
	return bs;
}


int table_dump(char *fn,const table_t *kmer_position_table, int arrayTypeTable)
{
	FILE *fp;

	fp = xopen(fn, "wb");
	if(arrayTypeTable == 1)
	{
		fwrite(kmer_position_table,sizeof(table_t),KMER_TABLE_SIZE,fp );
		fclose(fp);
		return 0;
	}

}

table_t *table_restore(const char *fn)
{
	table_t *kmerPositionTable;
	FILE *fp;

	kmerPositionTable = (table_t*)calloc(KMER_TABLE_SIZE, sizeof(table_t));
	if (kmerPositionTable == 0) {
			fprintf(stderr, "[dmap_aln] insufficient memory!\n");
			exit(1);
	}
	fp = xopen(fn, "rb");
	fseek(fp, 0, SEEK_SET);
	fread(kmerPositionTable, sizeof(table_t), KMER_TABLE_SIZE, fp);
	fclose(fp);
	return kmerPositionTable;
}


int indicatorArray_dump(char *fn,const uint8_t *kmerStrandIndicator)
{
	FILE *fp;

	fp = xopen(fn, "wb");
	fwrite(kmerStrandIndicator,sizeof(uint8_t),KMER_TABLE_SIZE/8,fp );
	fclose(fp);
	return 0;

}

uint8_t *indicatorArray_restore(const char *fn)
{
	uint8_t *kmerStrandIndicator;
	FILE *fp;

	kmerStrandIndicator = (uint8_t*)calloc(KMER_TABLE_SIZE/8, sizeof(uint8_t));
	if (kmerStrandIndicator == 0) {
				fprintf(stderr, "[dmap_aln] insufficient memory!\n");
				exit(1);
	}
	fp = xopen(fn, "rb");
	fseek(fp, 0, SEEK_SET);
	fread(kmerStrandIndicator, sizeof(uint8_t), KMER_TABLE_SIZE/8, fp);
	fclose(fp);
	return kmerStrandIndicator;
}


int writeSAM() {
	//Responsible to write sam file
	return 0;
}

